package cn.com.nes.site.service.product.impl;

import cn.com.nes.mybatis.agent.ope.entity.Sblx;
import cn.com.nes.mybatis.agent.ope.mapper.SblxMapper;
import cn.com.nes.site.entity.vo.product.GGoodsVO;
import cn.com.nes.site.service.product.SblxService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 设备类型管理实现类
 *
 * @Author: ShenWei
 * @Date: 2024/04/16/9:38
 */
@Slf4j
@Service
public class SblxServiceImpl extends ServiceImpl<SblxMapper, Sblx> implements SblxService {
    @Resource
    SblxMapper sblxMapper;

    @Override
    public void pushSblx(GGoodsVO vo, String sqlStr) {
        log.info("推送t_sblx主体数据:{},参数信息:{}", JSON.toJSONString(vo),JSON.toJSONString(sqlStr));
        try {
            // 查询设备类型的id
            Long id = null;
            Sblx oldSblx = sblxMapper.selectBySblxId(vo.getSblx());
            if (!Objects.isNull(oldSblx))
                id = oldSblx.getId();
            // 填充sblx主体信息
            Sblx sblx = new Sblx();
            sblx.setId(id);
            sblx.setSblxId(vo.getSblx()); // 设备类型id
            sblx.setSbmc(vo.getGoodsName()); // 设备名称
            sblx.setSbjc(vo.getGoodsName()); // 设备简称
            sblx.setSbcj(vo.getOperateUserOpeCompanyId()); // 设备厂家编码
            sblx.setSbxh(vo.getGoodsCode()); // 设备型号
            sblx.setSblx(vo.getSblx()); // 暂时存为设备类型id
            sblx.setSblxmc(vo.getGoodsName()); // 暂时存为设备类型名称
            sblx.setCpmc(vo.getGoodsName()); // 产品名称
            sblx.setManufactureId(vo.getOperateUserOpeCompanyId()); // 厂商id
            sblx.setCompanyId(vo.getCompanyId()); // 公司id
            sblx.setSfsc(Integer.valueOf(vo.getIsdelete())); // 是否删除
            sblx.setCrOper(vo.getCreateUid()); // 创建人
            sblx.setUpOper(vo.getModifyUid()); // 修改人
            sblx.setSyny(String.valueOf(vo.getEnergyType())); // 使用能源

            // 保存数据
            this.saveOrUpdate(sblx);
            // 获取id
            id = sblx.getId();

            // 设置设备参数
            if (StrUtil.isNotBlank(sqlStr))
                sblxMapper.updateFields(sqlStr,id);
        } catch (Exception e){
            log.error("推送t_sblx失败,错误详情:{}",e.getMessage());
        }
    }

    @Override
    public void delSblx(String sblxId, GGoodsVO vo) {
        log.info("删除sblxId:{},删除人:{}",sblxId,vo.getUserInfo().getId());
        try {
            // 组装修改数据
            Sblx sblx = new Sblx();
            sblx.setSfsc(1);
            sblx.setUpOper(vo.getUserInfo().getId());
            // 设置匹配规则
            QueryWrapper<Sblx> wrapper = new QueryWrapper<>();
            wrapper.eq("sblx_id",sblxId);
            // 实施更新操作
            boolean update = this.update(sblx, wrapper);
            log.info("删除情况:{}",update);
        } catch (Exception e){
            log.error("删除失败,错误详情:{}",e.getMessage());
        }
    }

    @Override
    public Sblx selectBySblxId(String sblxId) {
        return sblxMapper.selectBySblxId(sblxId);
    }

    @Override
    public List<Sblx> getDeviceModels(List<String> queryDeviceModelList) throws Exception {
        if (CollUtil.isEmpty(queryDeviceModelList)) {
            return Collections.emptyList();
        }

        QueryWrapper<Sblx> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sfsc", 0);
        queryWrapper.in("sblx_id",queryDeviceModelList);

        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<Map<String, String>> getManufacturerEquipmentInfo(String sbxh, List<String> manufacturerCompanyIdList) {
        return this.baseMapper.getManufacturerEquipmentInfo(sbxh, manufacturerCompanyIdList);
    }

}
